package cn.hhchat.wp.controller;

import cn.hhchat.wp.dto.msg.RMsg;
import cn.hhchat.wp.dto.msg.RMsgFactory;
import cn.hhchat.wp.entity.ConfigEntity;
import cn.hhchat.wp.entity.RoleEntity;
import cn.hhchat.wp.service.ConfigService;
import cn.hhchat.wp.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created this one by HMH on 2017/11/12.
 */
@RestController
public class InitController {

    @RequestMapping("/initAll")
    public RMsg init() {
        createConfigs();
        createRootUser();
        return RMsgFactory.ok();
    }

    @Autowired
    private RoleService roleService;

    @Autowired
    private ConfigService configService;

    public void createRootUser() {
        roleService.save(new RoleEntity("学生"));
        roleService.save(new RoleEntity("教师"));
        roleService.save(new RoleEntity("用户"));
        roleService.save(new RoleEntity("管理员"));
        roleService.save(new RoleEntity("系统管理员"));
    }

    public void createConfigs() {
        ConfigEntity allowReg = new ConfigEntity();
        allowReg.setName("allowReg");
        allowReg.setBoolValue(true);
        configService.save(allowReg);

        ConfigEntity allowLogin = new ConfigEntity();
        allowLogin.setName("allowLogin");
        allowLogin.setBoolValue(true);
        configService.save(allowLogin);
    }

}
